package application.view.gui;

import application.view.UserInterface;
import application.view.gui.MainView;
import application.view.gui.ReservationView;
import application.main.Application;

import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * An implementation of UserInterface creating a GUI based on Swing
 * 
 * @author Morten Therkildsen & Philip Rasmussen
 */
public class GraphicalUserInterface implements UserInterface {

	private MenuBuilder sharedMenuBuilder;
	
	/**
	 * Initialize the user interface - that is load it and make it run
	 */
	public void initialize(){
		
		sharedMenuBuilder = new MenuBuilder();
		addMenuItems();
		
		// Give each View a chance to do startup stuff before we launch windows
		// This will allow views to add global functionality they support to the shared menu bar
		MainView.startup(this);
		ReservationView.startup(this);
		
		// Open the first window
		MainView.open();
		
		// Now we are running!
	}
	
	/**
	 * Is used by views to build the shared menu bar
	 * 
	 * @return	a menu builder to be used across the application to add global menu items
	 */
	public MenuBuilder getSharedMenuBuilder(){
		
		return sharedMenuBuilder;
	}
	
	/**
	 * Adds a quit menu item to the shared menu
	 */
	private void addMenuItems(){
		
		JMenuItem quitItem = new JMenuItem("Quit");
		quitItem.addActionListener(new ActionListener(){
			
			public void actionPerformed(ActionEvent event){ Application.quit(); }
		});
		
		try{
			getSharedMenuBuilder().addMenuItem("File", quitItem);
		} catch(DuplicateMenuItemException e){/* IGNORE */}
	}
}
